﻿using System;
using System.Windows.Forms;
using System.Data;
using Gencode.PrsCommon.Data.Abteilung;

namespace Gencode.PrsUtil.Controls
{
	public enum PUAbteilungListViewType { All, Assigned };

	public class PUAbteilungList : ListBox
	{
		DataView m_abteilungen;
		int m_prsid;
		bool m_assigned = true;
		string m_mandid;
		PUAbteilungListViewType _viewType = PUAbteilungListViewType.All;

		public PUAbteilungList()
			: base()
		{
		}

		protected override void InitLayout()
		{
			base.InitLayout();
		}

		protected override void Dispose(bool disposing)
		{
			if (m_abteilungen != null)
				m_abteilungen.Dispose();

			base.Dispose(disposing);
		}

		public void InitData(int prsid, string mandid)
		{
			Mandid = mandid;
			PrsID = prsid;

			InitializeDataSource();
		}

		public void InitData(int prsid, string mandid, PUAbteilungListViewType viewtype)
		{
			Mandid = mandid;
			PrsID = prsid;
			_viewType = viewtype;

			InitializeDataSource();
		}

		public void InitializeDataSource()
		{
			DataTable dt = new DataTable();
			dt.Columns.Add("abteilungid");
			dt.Columns.Add("name");
			dt.Columns.Add("prozent");

			m_abteilungen = new DataView(dt);

			DataView dv;
			DataRow dr;
			switch (ViewType)
			{
				case PUAbteilungListViewType.All:
					if (!Assigned)
					{
						dv = AbteilungData.GetAbteilungen(Mandid);
						foreach (DataRow row in dv.Table.Rows)
						{
							dr = m_abteilungen.Table.NewRow();
							dr["abteilungid"] = row["abteilungid"];
							dr["name"] = row["name"];
							dr["prozent"] = "0";

							m_abteilungen.Table.Rows.Add(dr);
						}
					}
					break;

				case PUAbteilungListViewType.Assigned:
					dv = AbteilungData.GetAbteilungPerson(Mandid, PrsID, false, Assigned);
					foreach (DataRow row in dv.Table.Rows)
					{
						dr = m_abteilungen.Table.NewRow();
						dr["abteilungid"] = row["AbteilungID"];
						dr["name"] = row["Name"];
						dr["prozent"] = row["Prozent"];

						m_abteilungen.Table.Rows.Add(dr);
					}
					break;
			}

			DataSource = m_abteilungen;
			DisplayMember = "name";
		}

		public void AddItem(DataRow row, int prozent)
		{
			row["prozent"] = prozent.ToString();
			try
			{
				DataRow dr = m_abteilungen.Table.NewRow();
				dr["abteilungid"] = row["AbteilungID"];
				dr["name"] = row["Name"];
				dr["prozent"] = row["Prozent"];

				m_abteilungen.Table.Rows.Add(dr);
				//m_abteilungen.Table.Rows.Add(row);
			}
			catch { }
		}

		public void RemoveItem(DataRow row)
		{
			try
			{
				m_abteilungen.Table.Rows.Remove(row);
			}
			catch { }
		}

		public DataRow SelectedRow
		{
			get
			{
				if (SelectedIndex >= 0)
					if (m_abteilungen[SelectedIndex].Row["abteilungid"].ToString() != null &&
							m_abteilungen[SelectedIndex].Row["abteilungid"].ToString() != string.Empty)
						return m_abteilungen.Table.Rows[SelectedIndex];

				return null;
			}
		}

		public int PrsID
		{
			private get
			{
				return m_prsid;
			}
			set
			{
				m_prsid = value;
			}
		}

		public string Mandid
		{
			get
			{
				if (m_mandid == null)
					m_mandid = string.Empty;
				return m_mandid;
			}
			set
			{
				m_mandid = value;
			}
		}

		/// <summary>
		/// true = Abteilungen that are assigned to person; false = Abteilungen that are NOT assigned to person
		/// </summary>
		public bool Assigned
		{
			get
			{
				return m_assigned;
			}
			set
			{
				m_assigned = value;
			}
		}

		public int GetTotalPensum
		{
			get
			{
				int tmp = 0;
				foreach (DataRowView row in m_abteilungen)
				{
					tmp += int.Parse(row["prozent"].ToString());
				}
				return tmp;
			}
		}

		public int SelectedAbteilungID
		{
			get
			{
				if (SelectedIndex >= 0)
					if (m_abteilungen[SelectedIndex].Row["abteilungid"].ToString() != null &&
						m_abteilungen[SelectedIndex].Row["abteilungid"].ToString() != string.Empty)
						return int.Parse(m_abteilungen[SelectedIndex].Row["abteilungid"].ToString());

				return 0;
			}
		}

		public DataTable AbteilungDataTable
		{
			get
			{
				return m_abteilungen.Table;
			}
		}
				

		public PUAbteilungListViewType ViewType
		{
			get
			{
				return _viewType;
			}
			set
			{
				_viewType = value;
			}
		}

	}
}
